package org.jkuang.qstar.commons.jni;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.jkuang.qstar.commons.io.FileOperator;
import org.jkuang.qstar.commons.io.FileWriter;

public class RIndex extends Native {
	int module = integer.getAndIncrement();
	static AtomicInteger integer = new AtomicInteger(0);

	public RIndex(boolean compress, boolean ignoreCase) {
		create(module, ignoreCase, compress);
	}

	static String toString(long[] ids) {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < ids.length; i++) {
			buffer.append(ids[i]).append(",");
		}
		buffer.append('\n');
		return buffer.toString();
	}
	public static void main(String[] args) throws IOException {
		RIndex index = new RIndex(false, true);
		index.load("D:\\works\\dump\\reverse.101.bin");
		
		
		List<String> list = FileOperator.readFile(new File("D:\\works\\dump\\20171020.txt"));
        FileWriter writer = new FileWriter("D:\\works\\dump\\out01.txt");
        long time = System.currentTimeMillis();
        int count = 0;
       // String synxtax="(windcode:000019.SZ AND (sourcetype:2 OR sourcetype:1 OR sourcetype:0 OR sourcetype:7 OR sourcetype:6 OR sourcetype:5 OR sourcetype:4 OR sourcetype:9 OR sourcetype:8))";
        for (String key:list) {
        	try {
        		long[] ids =index.query(key , 0, 0, 0,100, 100, true);
    			if(count++%1000==999) {
    				System.out.println("count:"+count);
    			}
    			//writer.writeBytes(toString(ids).getBytes());
			} catch (Exception e) {
				System.out.println(key);
			}
		}
        System.out.println("---"+(System.currentTimeMillis()-time));
        writer.close();
   
	}

	public void insert(long key, long sort, Collection<String> terms) {
		if (terms == null || terms.size() == 0) {
			return;
		} else {
			addDoc(module, key, sort, terms.toArray(new String[0]));
		}
	}

	public void delete(long key) {
		delDoc(module, key);
	}

	public boolean dump(String fileName) {
		File file = new File(fileName);
		File dir = file.getParentFile();
		if (!dir.exists()) {
			dir.mkdirs();
		}
		dump(module, fileName);
		return true;
	}

	public boolean load(String fileName) throws IOException {
		File file = new File(fileName);
		if (file.exists() && file.length() > 1024) {
			int result = load(module, fileName);
			return result != 0;
		} else {
			return false;
		}
	}

	public long[] query(String syntax, long _s_sort, long _e_sort, int start, int rows, int count, boolean desc) {
		return query(module, syntax, _s_sort, _e_sort, start, rows, count, desc);
	}

	public void destroy() {
		destroy(module);
	}

	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		super.finalize();
		destroy();
	}

	private native int create(int index, boolean ignoreCase, boolean compress);

	private native int destroy(int index);

	private native int load(int index, String fileName);

	private native int dump(int index, String fileDir);

	private native int addDoc(int index, long key, long sort, String[] terms);

	private native int delDoc(int index, long key);

	private native long[] queryByKeys(int index, long[] keys);

	private native long[] query(int index, String syntax, long _s_sort, long _e_sort, int start, int rows, int count,
			boolean desc);
}
